/* Copyright (c) 2004-2023 Peigen.info. All rights reserved. */
package info.peigen.hotpot.manage.core.manage.service.export;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.enums.Symbol;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * <b>(ExportService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024/7/2
 */
@Component
public class ExportService {
    @Inject("${hotpot.component.export.export-path:/home/devops/files/export/}")
    String uploadPath;
    @Inject("${hotpot.component.export.download-path:https://static.hotpot.cn/files/export/}")
    String downloadPath;

    public ExportInfo export(List<?> list, String fileName) {
        XSSFWorkbook workbook = new XSSFWorkbook();

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);


        XSSFSheet sheet = workbook.createSheet("Sheet1");
        // 表头
        Row row0 = sheet.createRow(0);
        list.forEach(l -> {
            Field[] fields = ReflectUtil.getFields(l.getClass());
            int     i      = 0;
            for (Field field : fields) {
                if (StrUtil.equalsAny(field.getName(), "pageNum", "pageSize", "orderName", "desc", "createTimeStart", "createTimeEnd", "id", "partnerId", "createTime", "updateTime", "enable", "refId")) {
                    continue;
                }
                ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
                Cell             cell     = row0.createCell(i++);
                cell.setCellValue(property.value());
                cell.setCellStyle(cellStyle);
            }
        });

        // 内容
        list.forEach(l -> {
            Field[] fields = ReflectUtil.getFields(l.getClass());
            int     i      = 0;
            int     o      = 1;
            Row     row    = sheet.createRow(o++);
            for (Field field : fields) {
                if (StrUtil.equalsAny(field.getName(), "pageNum", "pageSize", "orderName", "desc", "createTimeStart", "createTimeEnd", "id", "partnerId", "createTime", "updateTime", "enable", "refId")) {
                    continue;
                }
                Cell cell = row.createCell(i++);
                try {
                    field.setAccessible(true);
                    cell.setCellValue(String.valueOf(field.get(l)));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                cell.setCellStyle(cellStyle);
            }
        });

        FileOutputStream os = null;
        fileName = fileName + Symbol.MINUS.code() + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        // uploadPath = StrUtil.replace(uploadPath, "\\\\\\", "\\");
        String fileUploadPath   = uploadPath + fileName + ".xlsx";
        String fileDownloadPath = downloadPath + fileName + ".xlsx";
        try {

            os = new FileOutputStream(fileUploadPath);
            workbook.write(os);
            os.close();
            return ExportInfo.builder().filePath(fileDownloadPath).fileName(fileName).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
